﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Video
    {
         #region Common
        public eVideo Data;

        public Video()
        {
            Data = new eVideo();
		}

        ~Video()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cVideo objDAL = new cVideo();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cVideo objDAL = new cVideo();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cVideo objInfor = new cVideo(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.VideoID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }
        //Update nhung cai ma eVideo Data dang nam giu thong qua cVideo objInfor
        public int Update(int ID)
        {
            int nResult = 0;
            cVideo objInfor = new cVideo();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cVideo objDAL = new cVideo();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.VideoID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int UpdatePriority(int VideoID, int Priority)
        {
            int nResult = 0;
            cVideo objDAL = new cVideo();
            try
            {
                objDAL.LoadByPrimaryKey(VideoID);
                objDAL.Priority = Priority;

                objDAL.Save();
                nResult = objDAL.VideoID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        private void MappingData(cVideo objInfor)
        {
            objInfor.CreatedDate = Data.CreatedDate;

            try { objInfor.InitContent1 = Data.InitContent1; }
            catch { objInfor.InitContent1 = ""; }

            try { objInfor.InitContent2 = Data.InitContent2; }
            catch { objInfor.InitContent2 = ""; }

            try { objInfor.InitContent3 = Data.InitContent3; }
            catch { objInfor.InitContent3 = ""; }

            try{objInfor.Priority = Data.Priority;}
            catch{objInfor.Priority = 0;}
            
            try{objInfor.Status = Data.Status;}
            catch{objInfor.Status = 0;}

            try{objInfor.TourID = Data.TourID;}
            catch
            {objInfor.TourID = 0;}
            
            try{objInfor.URL = Data.URL;}
            catch
            {objInfor.URL = "";}
            
            try{objInfor.VideoTitle1 = Data.VideoTitle1;}
            catch{objInfor.VideoTitle1 = "";}
            
            try{objInfor.VideoTitle2 = Data.VideoTitle2;}
            catch{objInfor.VideoTitle2 = "";}
            
            try{objInfor.VideoTitle3 = Data.VideoTitle3;}
            catch{objInfor.VideoTitle3 = "";}
        }
        private void ConvertData(cVideo objInfor)
        {
            Data.CreatedDate = objInfor.CreatedDate;
            Data.InitContent1 = objInfor.InitContent1;
            Data.InitContent2 = objInfor.InitContent2;
            Data.InitContent3 = objInfor.InitContent3;
            Data.Priority = objInfor.Priority;
            Data.Status = objInfor.Status;
            Data.TourID = objInfor.TourID;
            Data.URL = objInfor.URL;
            Data.VideoTitle1 = objInfor.VideoTitle1;
            Data.VideoTitle2 = objInfor.VideoTitle2;
            Data.VideoTitle3 = objInfor.VideoTitle3;
        }
        public DataTable GetAll()
        {
            DataTable tbResult = new DataTable();
            cVideo objDAL = new cVideo();
            tbResult = objDAL.GetAll();
            return tbResult;
        }
        public DataTable Search(int nItemView, int nItemID, string sSearch, int nDefaultLanguage, int nLanguageID, int nStatus, int CurrentPageIndex,
            int nPageSize, ref int nPageCount)
        {
            DataTable tbResult = new DataTable();
            cVideo objDAL = new cVideo();
            tbResult = objDAL.Search(nItemView, nItemID, sSearch, nDefaultLanguage, nLanguageID, nStatus, CurrentPageIndex,
            nPageSize, ref nPageCount);
            return tbResult;
        }
         #endregion
    }
}
